//--------------------------------------------------------------------------------------
// File: SimpleSample.hlsl
//
// The HLSL file for the SimpleSample sample for the Direct3D 11 device
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------


//--------------------------------------------------------------------------------------
// Constant Buffers
//--------------------------------------------------------------------------------------
cbuffer cbPerObject : register( b0 )
{
    matrix  g_mWorldViewProjection  : packoffset( c0 );
    matrix  g_mWorld                : packoffset( c4 );
    float4  g_MaterialAmbientColor  : packoffset( c8 );
    float4  g_MaterialDiffuseColor  : packoffset( c9 );
}

cbuffer cbPerFrame : register( b1 )
{
    float3              g_vEye             	: packoffset( c0 );
    float               g_fTime				: packoffset( c0.w );
    float4              g_LightDiffuse		: packoffset( c1 );
};

//-----------------------------------------------------------------------------------------
// Textures and Samplers
//-----------------------------------------------------------------------------------------
Texture2D    g_txDiffuse : register( t0 );
SamplerState g_samLinear : register( s0 );

//--------------------------------------------------------------------------------------
// shader input/output structure
//--------------------------------------------------------------------------------------
struct VS_INPUT
{
    float4 Position     : POSITION; // vertex position 
    float2 TextureUV    : TEXCOORD0;// vertex texture coords 
	float  skyLight		: TEXCOORD1;// skylight
	float  blockLight	: TEXCOORD2;// blocklight
};

struct VS_OUTPUT
{
    float4 Position     : SV_POSITION; // vertex position 
    float4 Diffuse      : COLOR0;      // vertex diffuse color (note that COLOR0 is clamped from 0..1)
    float2 TextureUV    : TEXCOORD0;   // vertex texture coords 
	float  Fog			: TEXCOORD1;   // fog value
};

//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( VS_INPUT input )
{
    VS_OUTPUT Output;
    //float3 vNormalWorldSpace;
	
	float4 cameraPosition;
	cameraPosition = mul(input.Position, g_mWorld);
	
	Output.Fog = saturate((180.0f - cameraPosition.z) / 100.0f);
    
    // Transform the position from object space to homogeneous projection space
    Output.Position = mul( input.Position, g_mWorldViewProjection );
    
    // Transform the normal from object space to world space    
    //vNormalWorldSpace = input.Normal;//normalize(mul(input.Normal, (float3x3)g_mWorld)); // normal (world space)

    // Calc diffuse color 
	float3 light = clamp((input.skyLight) * (g_LightDiffuse.xyz), 0.005f, 5.0f);

	float3 torch = float3(1.2f, 1.0f, 0.7f) * input.blockLight;

    Output.Diffuse.rgb = light + torch;//clamp(light + torch, 0.01f, 2.0f);

							//g_MaterialDiffuseColor * g_LightDiffuse * max(0,dot(vNormalWorldSpace, g_vLightDir)) + 
							//g_MaterialAmbientColor;

    Output.Diffuse.a = 1.0f; 
    
    // Just copy the texture coordinate through
    Output.TextureUV = input.TextureUV; 
    
    return Output;    
}

//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
float4 RenderScenePS( VS_OUTPUT In ) : SV_TARGET
{ 
    // Lookup mesh texture and modulate it with diffuse

	float4 fogColor = float4( 0.176f, 0.196f, 0.667f, 1.0f);
	float4 textureColor = In.Diffuse * g_txDiffuse.Sample( g_samLinear, In.TextureUV );
    return In.Fog * textureColor + (1.0 - In.Fog) * fogColor;
}
